{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3363d3ad",
   "metadata": {},
   "source": [
    "## 1.音频录制\n",
    "\n",
    "* 安装下载如下3个模块：pip install 模块名称\n",
    "> 1. SpeechRecognition\n",
    "> 2. Pyaudio\n",
    "> 3. Pydub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f1e4728d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8a6db19",
   "metadata": {},
   "source": [
    "## 2.语音识别\n",
    "> 1.百度API：百度的AI账户--（Key，Secret）\n",
    "\n",
    "> 2.I/O：input:音频文件（pcm/wav/amr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3a797729",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "45af120d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"refresh_token\":\"25.d98ae20615cd2c3511f50b07d6c24394.315360000.2002158583.282335-33416066\",\"expires_in\":2592000,\"session_key\":\"9mzdDoD0a69DHrrnhcgcafJbnzkrDfZIvoD\\/7loRKb9\\/qQ5hDHbtFV9gSEsYQSLxbN4ts5SWW4Sk92j+DexfrBe9RtRECw==\",\"access_token\":\"24.52e88addb368ba2fbbc14497b1e650f0.2592000.1689390583.282335-33416066\",\"scope\":\"audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\\u6743\\u9650 vis-classify_flower lpq_\\u5f00\\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\\u5f00\\u653eScope vis-ocr_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 idl-video_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx\",\"session_secret\":\"6be9b3512ac2984b0dcc5c214609b7ca\"}\n",
      "\n",
      "{'refresh_token': '25.d98ae20615cd2c3511f50b07d6c24394.315360000.2002158583.282335-33416066', 'expires_in': 2592000, 'session_key': '9mzdDoD0a69DHrrnhcgcafJbnzkrDfZIvoD/7loRKb9/qQ5hDHbtFV9gSEsYQSLxbN4ts5SWW4Sk92j+DexfrBe9RtRECw==', 'access_token': '24.52e88addb368ba2fbbc14497b1e650f0.2592000.1689390583.282335-33416066', 'scope': 'audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': '6be9b3512ac2984b0dcc5c214609b7ca'}\n",
      "audio_voice_assistant_get\n",
      "SUCCESS WITH TOKEN: 24.52e88addb368ba2fbbc14497b1e650f0.2592000.1689390583.282335-33416066  EXPIRES IN SECONDS: 2592000\n",
      "Request time cost 0.638881\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'{\"corpus_no\":\"7244744750322533529\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"北京科技馆。\"],\"sn\":\"328649243331686798583\"}\\n'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bdasr.asr('16k.wav')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c91605a0",
   "metadata": {},
   "source": [
    "## 3.连续自动回复字典（openai-chatgpt）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4d55fc49",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7abb5a42",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"refresh_token\":\"25.0efc085e513e89cc4594fa508381cbd8.315360000.2002158584.282335-33416066\",\"expires_in\":2592000,\"session_key\":\"9mzdCKJGsn\\/cDWzntRSggTmSlKJm+96+qhiiRIvO0PXl1SXEGRQ0TgTEiq1tZ9RgI5eaaksfB6EFKWx\\/GYszg4FAVjq64A==\",\"access_token\":\"24.3f16242a247c4d9ee868b0dd121dc354.2592000.1689390584.282335-33416066\",\"scope\":\"audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\\u6743\\u9650 vis-classify_flower lpq_\\u5f00\\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\\u5f00\\u653eScope vis-ocr_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 idl-video_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx\",\"session_secret\":\"5c017f448ab1007721a7ae386569dcdb\"}\n",
      "\n",
      "{'refresh_token': '25.0efc085e513e89cc4594fa508381cbd8.315360000.2002158584.282335-33416066', 'expires_in': 2592000, 'session_key': '9mzdCKJGsn/cDWzntRSggTmSlKJm+96+qhiiRIvO0PXl1SXEGRQ0TgTEiq1tZ9RgI5eaaksfB6EFKWx/GYszg4FAVjq64A==', 'access_token': '24.3f16242a247c4d9ee868b0dd121dc354.2592000.1689390584.282335-33416066', 'scope': 'audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': '5c017f448ab1007721a7ae386569dcdb'}\n",
      "audio_voice_assistant_get\n",
      "SUCCESS WITH TOKEN: 24.3f16242a247c4d9ee868b0dd121dc354.2592000.1689390584.282335-33416066  EXPIRES IN SECONDS: 2592000\n",
      "Request time cost 0.933108\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bdasr_result  = eval(bdasr.asr('1.wav'))['result'][0]\n",
    "bdasr_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f5afd011",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    \"你是谁？\":\"我是集美貌和智慧于一体的智能小网同学\",\n",
    "    '你好。':\"你也好，今天听起来心情不错哦\",\n",
    "    '北京科技馆。':\"中国科学技术馆，位于北京市朝阳区北辰东路5号，东临亚运居住区，西濒奥运水系，南依奥运主体育场，北望森林公园，于1958年筹建，建设分为两期工程和新馆工程，分别于1988年9月22日、2000年4月29日、2009年9月16日开放。\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cb8f82e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa_result = qa.get(bdasr_result)\n",
    "qa_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ed227f0",
   "metadata": {},
   "source": [
    "### 3.1 接入chatgpt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bd7ac2ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: openai in c:\\programdata\\anaconda3\\lib\\site-packages (0.27.6)\n",
      "Requirement already satisfied: tqdm in c:\\programdata\\anaconda3\\lib\\site-packages (from openai) (4.59.0)\n",
      "Requirement already satisfied: requests>=2.20 in c:\\users\\86158\\appdata\\roaming\\python\\python38\\site-packages (from openai) (2.30.0)\n",
      "Requirement already satisfied: aiohttp in c:\\programdata\\anaconda3\\lib\\site-packages (from openai) (3.8.4)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from requests>=2.20->openai) (1.26.4)\n",
      "Requirement already satisfied: idna<4,>=2.5 in c:\\programdata\\anaconda3\\lib\\site-packages (from requests>=2.20->openai) (2.10)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in c:\\programdata\\anaconda3\\lib\\site-packages (from requests>=2.20->openai) (2020.12.5)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\programdata\\anaconda3\\lib\\site-packages (from requests>=2.20->openai) (3.1.0)\n",
      "Requirement already satisfied: attrs>=17.3.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from aiohttp->openai) (20.3.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from aiohttp->openai) (1.3.3)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in c:\\programdata\\anaconda3\\lib\\site-packages (from aiohttp->openai) (1.3.1)\n",
      "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in c:\\programdata\\anaconda3\\lib\\site-packages (from aiohttp->openai) (4.0.2)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from aiohttp->openai) (1.9.2)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\programdata\\anaconda3\\lib\\site-packages (from aiohttp->openai) (6.0.4)\n"
     ]
    }
   ],
   "source": [
    "!pip install openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1179a95a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import opchat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5598a29a",
   "metadata": {},
   "source": [
    "## 4.语音合成\n",
    "> 1.百度API:百度的AI账户--（Key，Secret）\n",
    "\n",
    "> 2.I/O：input：文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50a91165",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdtts\n",
    "bdtts.tts(qa_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b311ff24",
   "metadata": {},
   "source": [
    "## 5.播放音频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e8b69a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
